﻿using System;
using System.Diagnostics;
using Microsoft.ApplicationServer.Caching;
namespace MDCAdminTool
{
    public static class CacheClientFactory
    {
        public static DataCache GetCache( MdcCache cache )
        {
            return GetCache( cache.Name , "localhost" , cache.Parent.CachePort , cache.Parent.ServiceName );
        }
        public static DataCache GetCache( string cacheName , string serverName , int port , string serviceName )
        {
            var servers = new[] { new DataCacheServerEndpoint( serverName , port ) };

            //DataCacheFactory.DisableLogSinks();

            var configuration = 
                new DataCacheFactoryConfiguration
                {
                    Servers = servers,
                    LocalCacheProperties = new DataCacheLocalCacheProperties()
                };

            DataCacheClientLogManager.ChangeLogLevel( TraceLevel.Off );

            try
            {
                var dcf = new DataCacheFactory( configuration );

                var dcc = dcf.GetCache( cacheName );

                return dcc;
            }
            catch (Exception exception)
            {
                //TODO: Log exception.
            }

            return null;
        }
    }
}
